<!--计量单位选择框-->
<template>
  <div>
    <BasicModal v-bind="$attrs" @register="register" :title="modalTitle" width="1024px" :min-height="500" @ok="handleOk"
      destroyOnClose @visible-change="visibleChange">
      <BasicTable toolbar 
      row-number
      :height="500"  :columns="columns" v-bind="config" :useSearchForm="true" 
      :formConfig="formConfig" 
      :api="orderList"
        :searchInfo="searchInfo" :rowSelection="rowSelection" ></BasicTable>
    </BasicModal>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, unref } from "vue";
import { BasicModal, useModalInner } from "/@/components/Modal";
import { orderList } from "./base.api";
import { createAsyncComponent } from "/@/utils/factory/createAsyncComponent";
import { useSelectBiz } from "/@/components/Form/src/jeecg/hooks/useSelectBiz";
import { selectProps } from "/@/components/Form/src/jeecg/props/props";
import { useAttrs } from "/@/hooks/core/useAttrs";

export default defineComponent({
  name: "CalculateSelectModal",
  components: {
    //此处需要异步加载BasicTable
    BasicModal,
    BasicTable: createAsyncComponent(
      () => import("/@/components/Table/src/BasicTable.vue"),
      {
        loading: true,
      }
    ),
  },
  props: {
    ...selectProps,
    //选择框标题
    modalTitle: {
      type: String,
      default: "订单选择",
    },
    multiple: {
      type: Boolean,
      default: true,
    },
    isNotPlaned: {
      type: Number,
      default: 1
    },
    isMake: {
      type: Number,
      default: 1
    },
    queryType: {
      type: Number,
      default: 0
    },
  },
  emits: ["register", "getSelectResult"],
  setup(props, { emit }) {
    let type: number = 0;
    //注册弹框
    const [register, { closeModal }] = useModalInner(async (data) => {
      type = data.type;
    });
    const attrs = useAttrs();
    //表格配置
    const config = {
      canResize: true,
      bordered: true,
      size: "small",
      rowKey: unref(props).rowKey,
      labelKey: "schedulingSchemeName",
      isRadioSelection: !(props.multiple == true) ?? true,
      // 请求之前对参数做处理
      beforeFetch(params) {
        // 供应类型为自制
        params.supplyType = 0;
        // 过滤掉订单数量-已排产数量大于0的数据
        params.productionPlanQuery = 1;
        params.isNotPlaned = 1;
        params.isMake = 1;
        params.queryType = props.queryType
      },
    };
    const getBindValue = Object.assign({}, unref(props), unref(attrs), config);
    const [
      { rowSelection, indexColumnProps, visibleChange, getSelectResult, reset },
    ] = useSelectBiz(orderList, getBindValue);
    const searchInfo = ref(props.params);

    //查询form
    const formConfig = {
      labelWidth: 80,
      baseColProps: {
        xs: 8,
        sm: 8,
      },
      schemas: [
        {
          label: '订单来源',
          field: 'orderChannel',
          component: 'Select',
          componentProps: {
            options: [{
              label: '国内订单',
              value: 0
            }, {
              label: '海外订单',
              value: 1
            }, {
              label: '研发订单',
              value: 2
            }, {
              label: '售后订单',
              value: 3
            }],
            stringToNumber: true,
          },
          colProps: { span: 8 },
        },
        {
          label: "订单编码",
          field: "billCode",
          component: "JInput",
          colProps: { span: 8 },
        },
        {
          label: '物料编码',
          field: 'materialCode',
          component: 'JInput',
          colProps: { span: 8 },
        },
        {
          label: "订单交期",
          field: "orderDeliveryDate",
          component: "RangePicker",
          componentProps: {
            valueType: "Date",
            getPopupContainer: () => document.body,
          },
          colProps: { span: 8 },
        },
      ],
    };
    //   useModal((data: any) => {
    //     console.log(data);
    //   });
    //定义表格列
    const columns = [
      {
        title: "订单号",
        dataIndex: "billCode",
        width: 160,
        align: "left",
        fixed: 'left',
      },
      {
        title: "行号",
        dataIndex: "rowNum",
        width: 60,
        align: "left",
        fixed: 'left',
      },
      {
        title: "订单类型",
        dataIndex: "billTypeName",
        width: 80,
      },
      {
        title: "订单来源",
        dataIndex: "orderChannel",
        width: 80,
        customRender: ({ text }) => {
          if(text==0) return '国内订单'
          if(text==1) return '国外订单'
          if(text==2) return '研发订单'
          if(text==3) return '售后订单'

          return '未知来源'
        },
      },
      {
        title: "供应类型",
        dataIndex: "supplyType",
        width: 80,
        customRender: ({ text }) => {
          if(text==0) return '自制'
          if(text==1) return '采购'
          if(text==2) return '委外'

          return '未知'
        },
      },
      {
        title: "是否OEM",
        dataIndex: "oemNot",
        width: 80,
        customRender: ({ text }) => {
          if(text==0) return '否'
          if(text==1) return '是'
          return '未知'
        },
      },
      {
        title: "料品编码",
        dataIndex: "materialCode",
        width: 120,
      },
      {
        title: "料品名称",
        dataIndex: "materialName",
        width: 120,
      },
      {
        title: "图号",
        dataIndex: "drawingNo",
        width: 120,
      },
      {
        title: "订单数量",
        dataIndex: "orderQuantity",
        width: 80,
      },
      {
        title: "已排数量",
        dataIndex: "productQuantity",
        width: 80,
      },
      {
        title: "订单交期",
        dataIndex: "orderDeliveryDate",
        width: 120,
      },
      {
        title: "备注",
        dataIndex: "remark",
      },
      {
        title: "创建时间",
        dataIndex: "createTime",
      },
    ];
    /**
     * 确定选择
     */
    function handleOk() {
      getSelectResult((options, values) => {
        //回传选项和已选择的值
        emit("getSelectResult", options, values, type);
        //关闭弹窗
        closeModal();
        // 清空选择器的选择
        reset();
      });
    }

    return {
      config,
      handleOk,
      searchInfo,
      register,
      indexColumnProps,
      visibleChange,
      orderList,
      formConfig,
      getBindValue,
      columns,
      rowSelection,
    };
  },
});
</script>
